home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Sample.bin / SimpleSortTableModel.java < prev    next >
Text File  |  1998-09-28  |  11KB  |  387 lines

  1. import com.sun.java.swing.*;
  2. import com.sun.java.swing.table.*;
  3. import com.sun.java.swing.event.*;
  4.  
  5. import java.awt.event.*;
  6. import java.io.*;
  7. import java.beans.*;
  8. import java.util.*;
  9.  
  10. public class SimpleSortTableModel extends com.sun.java.swing.table.AbstractTableModel implements java.io.Serializable, TableModelListener
  11. {
  12.     TableModel model=null;
  13.     
  14.     int[] indexes = {};
  15.     Vector sortingColumns = new Vector();
  16.     boolean ascending=true;
  17.     int compares;
  18.     
  19.     public SimpleSortTableModel()
  20.     {
  21.     }
  22.  
  23.     // There is no-where else to put this. 
  24.     // Add a mouse listener to the Table to trigger a table sort 
  25.     // when a column heading is clicked in the JTable. 
  26.     public void addMouseListenerToHeaderInTable(JTable table) 
  27.     { 
  28.         final SimpleSortTableModel sorter = this; 
  29.         final JTable tableView = table; 
  30.         tableView.setColumnSelectionAllowed(false); 
  31.         MouseAdapter listMouseListener = new MouseAdapter() 
  32.         {
  33.             public void mouseClicked(MouseEvent e) 
  34.             {
  35.                 TableColumnModel columnModel = tableView.getColumnModel();
  36.                 int viewColumn = columnModel.getColumnIndexAtX(e.getX()); 
  37.                 int column = tableView.convertColumnIndexToModel(viewColumn); 
  38.                 if(e.getClickCount() == 1 && column != -1) 
  39.                 {
  40.                     System.out.println("Sorting ..."); 
  41.                     int shiftPressed = e.getModifiers()&InputEvent.SHIFT_MASK; 
  42.                     boolean ascending = (shiftPressed == 0); 
  43.                     sorter.sortByColumn(column, ascending); 
  44.                 }
  45.              }
  46.          };
  47.         JTableHeader th = tableView.getTableHeader(); 
  48.         th.addMouseListener(listMouseListener); 
  49.     }
  50.  
  51.     public void reallocateIndexes()
  52.     {
  53.         int rowCount = model.getRowCount();
  54.  
  55.         // Set up a new array of indexes with the right number of elements
  56.         // for the new data model.
  57.         indexes = new int[rowCount];
  58.  
  59.         // Initialise with the identity mapping.
  60.         for(int row = 0; row < rowCount; row++)
  61.         {
  62.             indexes[row] = row;
  63.         }
  64.     }
  65.  
  66.     public int compareRowsByColumn(int row1, int row2, int column)
  67.     {
  68.         Class type = model.getColumnClass(column);
  69.         TableModel data = model;
  70.  
  71.         // Check for nulls
  72.  
  73.         Object o1 = data.getValueAt(row1, column);
  74.         Object o2 = data.getValueAt(row2, column); 
  75.  
  76.         // If both values are null return 0
  77.         if (o1 == null && o2 == null) 
  78.         {
  79.             return 0; 
  80.         }
  81.         else if (o1 == null) 
  82.         { // Define null less than everything. 
  83.             return -1; 
  84.         } 
  85.         else if (o2 == null) 
  86.         { 
  87.             return 1; 
  88.         }
  89.  
  90. /* We copy all returned values from the getValue call in case
  91. an optimised model is reusing one object to return many values.
  92. The Number subclasses in the JDK are immutable and so will not be used in 
  93. this way but other subclasses of Number might want to do this to save 
  94. space and avoid unnecessary heap allocation. 
  95. */
  96.         if (type.getSuperclass() == java.lang.Number.class)
  97.         {
  98.             Number n1 = (Number)data.getValueAt(row1, column);
  99.             double d1 = n1.doubleValue();
  100.             Number n2 = (Number)data.getValueAt(row2, column);
  101.             double d2 = n2.doubleValue();
  102.  
  103.             if (d1 < d2)
  104.             {
  105.                 return -1;
  106.             }
  107.             else if (d1 > d2)
  108.             {
  109.                 return 1;
  110.             }
  111.             else
  112.             {
  113.                 return 0;
  114.             }
  115.         }
  116.         else if (type == java.util.Date.class)
  117.         {
  118.             Date d1 = (Date)data.getValueAt(row1, column);
  119.             long n1 = d1.getTime();
  120.             Date d2 = (Date)data.getValueAt(row2, column);
  121.             long n2 = d2.getTime();
  122.  
  123.             if (n1 < n2)
  124.             {
  125.                 return -1;
  126.             }
  127.             else if (n1 > n2)
  128.             {
  129.                 return 1;
  130.             }
  131.             else 
  132.             {
  133.                 return 0;
  134.             }
  135.         }
  136.         else if (type == String.class)
  137.         {
  138.             String s1 = (String)data.getValueAt(row1, column);
  139.             String s2    = (String)data.getValueAt(row2, column);
  140.             int result = s1.compareTo(s2);
  141.  
  142.             if (result < 0)
  143.             {
  144.                 return -1;
  145.             }
  146.             else if (result > 0)
  147.             {
  148.                 return 1;
  149.             }
  150.             else 
  151.             {
  152.                 return 0;
  153.             }
  154.         }
  155.         else if (type == Boolean.class)
  156.         {
  157.             Boolean bool1 = (Boolean)data.getValueAt(row1, column);
  158.             boolean b1 = bool1.booleanValue();
  159.             Boolean bool2 = (Boolean)data.getValueAt(row2, column);
  160.             boolean b2 = bool2.booleanValue();
  161.  
  162.             if (b1 == b2)
  163.             {
  164.                 return 0;
  165.             }
  166.             else if (b1) // Define false < true
  167.             {
  168.                 return 1;
  169.             }
  170.             else
  171.             {
  172.                 return -1;
  173.             }
  174.         }
  175.         else
  176.         {
  177.             Object v1 = data.getValueAt(row1, column);
  178.             String s1 = v1.toString();
  179.             Object v2 = data.getValueAt(row2, column);
  180.             String s2 = v2.toString();
  181.             int result = s1.compareTo(s2);
  182.  
  183.             if (result < 0)
  184.             {
  185.                 return -1;
  186.             }
  187.             else if (result > 0)
  188.             {
  189.                 return 1;
  190.             }
  191.             else 
  192.             {
  193.                 return 0;
  194.             }
  195.         }
  196.     }
  197.  
  198.     public int compare(int row1, int row2)
  199.     {
  200.         compares++;
  201.         for(int level = 0; level < sortingColumns.size(); level++)
  202.         {
  203.             Integer column = (Integer)sortingColumns.elementAt(level);
  204.             int result = compareRowsByColumn(row1, row2, column.intValue());
  205.             if (result != 0)
  206.             {
  207.                 return ascending ? result : -result;
  208.             }
  209.         }
  210.         return 0;
  211.     }
  212.  
  213.     public void checkModel()
  214.     {
  215.         if (indexes.length != model.getRowCount()) 
  216.         {
  217.             System.err.println("Sorter not informed of a change in model.");
  218.         }
  219.     }
  220.  
  221.     public void  sort(Object sender)
  222.     {
  223.         checkModel();
  224.  
  225.         compares = 0;
  226.         // n2sort();
  227.         // qsort(0, indexes.length-1);
  228.         shuttlesort((int[])indexes.clone(), indexes, 0, indexes.length);
  229.         System.out.println("Compares: "+compares);
  230.     }
  231.     public void n2sort() 
  232.     {
  233.         for(int i = 0; i < getRowCount(); i++) 
  234.         {
  235.             for(int j = i+1; j < getRowCount(); j++) 
  236.             {
  237.                 if (compare(indexes[i], indexes[j]) == -1) 
  238.                 {
  239.                     swap(i, j);
  240.                 }
  241.             }
  242.         }
  243.     }
  244.  
  245.     // This is a home-grown implementation which we have not had time
  246.     // to research - it may perform poorly in some circumstances. It
  247.     // requires twice the space of an in-place algorithm and makes
  248.     // NlogN assigments shuttling the values between the two
  249.     // arrays. The number of compares appears to vary between N-1 and
  250.     // NlogN depending on the initial order but the main reason for
  251.     // using it here is that, unlike qsort, it is stable.
  252.     public void shuttlesort(int from[], int to[], int low, int high) 
  253.     {
  254.         if (high - low < 2) 
  255.         {
  256.             return;
  257.         }
  258.         int middle = (low + high)/2;
  259.         shuttlesort(to, from, low, middle);
  260.         shuttlesort(to, from, middle, high);
  261.  
  262.         int p = low;
  263.         int q = middle;
  264.  
  265.         /* This is an optional short-cut; at each recursive call,
  266.         check to see if the elements in this subset are already
  267.         ordered.  If so, no further comparisons are needed; the
  268.         sub-array can just be copied.  The array must be copied rather
  269.         than assigned otherwise sister calls in the recursion might
  270.         get out of sinc.  When the number of elements is three they
  271.         are partitioned so that the first set, [low, mid), has one
  272.         element and and the second, [mid, high), has two. We skip the
  273.         optimisation when the number of elements is three or less as
  274.         the first compare in the normal merge will produce the same
  275.         sequence of steps. This optimization seems to be worthwhile
  276.         for partially ordered lists but some analysis is needed to
  277.         find out how the performance drops to Nlog(N) as the initial
  278.         order diminishes - it may drop very quickly.  */
  279.  
  280.         if (high - low >= 4 && compare(from[middle-1], from[middle]) <= 0) 
  281.         {
  282.             for (int i = low; i < high; i++) 
  283.             {
  284.                 to[i] = from[i];
  285.             }
  286.             return;
  287.         }
  288.  
  289.         // A normal merge. 
  290.  
  291.         for(int i = low; i < high; i++) 
  292.         {
  293.             if (q >= high || (p < middle && compare(from[p], from[q]) <= 0)) 
  294.             {
  295.                 to[i] = from[p++];
  296.             }
  297.             else 
  298.             {
  299.                 to[i] = from[q++];
  300.             }
  301.         }
  302.     }
  303.  
  304.     public void swap(int i, int j) 
  305.     {
  306.         int tmp = indexes[i];
  307.         indexes[i] = indexes[j];
  308.         indexes[j] = tmp;
  309.     }
  310.  
  311.     public void sortByColumn(int column) 
  312.     {
  313.         sortByColumn(column, true);
  314.     }
  315.  
  316.     public void sortByColumn(int column, boolean ascending) 
  317.     {
  318.         this.ascending = ascending;
  319.         sortingColumns.removeAllElements();
  320.         sortingColumns.addElement(new Integer(column));
  321.         sort(this);
  322.         fireTableChanged(new TableModelEvent(this));
  323.     }
  324.  
  325. //Properties:
  326.     public void setModel(TableModel tm)
  327.     {
  328.         if (model!=null)
  329.         {
  330.             model.removeTableModelListener(this);
  331.         }
  332.         model = tm;
  333.         model.addTableModelListener(this);
  334.         
  335.         reallocateIndexes();
  336.     }
  337.     public TableModel getModel()
  338.     {
  339.         return model;
  340.     }
  341.  
  342. //TableModel methods:
  343.     public Object getValueAt(int row, int col)
  344.     {
  345.         checkModel();
  346.         return model.getValueAt(indexes[row], col);
  347.     }
  348.     public void setValueAt(Object value, int row, int col)
  349.     {
  350.         checkModel();
  351.         model.setValueAt(value,indexes[row],col);
  352.     }
  353.  
  354.     public int getRowCount()
  355.     {
  356.         return model==null ? 0 : model.getRowCount();
  357.     }
  358.  
  359.     public int getColumnCount()
  360.     {
  361.         return model==null ? 0 : model.getColumnCount();
  362.     }
  363.     
  364.     public String getColumnName(int col)
  365.     {
  366.         return model.getColumnName(col);
  367.     }
  368.     
  369.     public Class getColumnClass(int col)
  370.     {
  371.         return model.getColumnClass(col);
  372.     }
  373.  
  374.     public boolean isCellEditable(int row, int col)
  375.     {
  376.         return model.isCellEditable(indexes[row], col);
  377.     }
  378.     
  379. // Implementation of the TableModelListener interface, 
  380.  
  381.     public void tableChanged(TableModelEvent e)
  382.     {
  383.         System.out.println("SimpleSortTableModel:table changed");
  384.         reallocateIndexes();
  385.         fireTableChanged(e);
  386.     }
  387. }